﻿package
	{
	import flash.text.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.ui.*;
	import flash.utils.*;
	
	public class MainClass extends MovieClip
		{
		var map:Map;
		var client:Client;
		var player:Player;
		var allPlayers:Object;
		//Arrow pressed state : left, up, right, down, shift, ctrl
		var keyHandler:Object = {37:false, 38:false, 39:false, 40:false, 16:false, 17:false, "m":false, "z":false};
		var config:ServerConfig = new ServerConfig();
		var car:Vehicule;
		
		static const toRadian:Number = Math.PI/180;
		static const toDegree:Number = 180/Math.PI;
		
		public function MainClass()
			{
			stage.addChild(config);
			config.go.addEventListener(MouseEvent.CLICK, run);
			stage.addEventListener(Event.DEACTIVATE, windowNotActive);
			stage.addEventListener(Event.ACTIVATE, windowActive);
			}
		
		public function windowNotActive(e:Event):void
			{
			for (var i in keyHandler)
				keyHandler[i] = false;
			}

		public function windowActive(e:Event):void
			{
			}
		
		public static function cloneMC(toClone:MovieClip):MovieClip
			{
			var targetClass:Class = Object(toClone).constructor;
			var cloned:MovieClip = new targetClass();
			cloned.gotoAndStop(toClone.currentFrame);
			return cloned;
			}
		
		public static function loadMC(reference:String):MovieClip
			{
			var ClassReference:Class = getDefinitionByName(reference) as Class;
            return new ClassReference() as MovieClip;
			}
		
		public function run(e:MouseEvent)
			{
			var adress:String = config.adress.text;
			var port:Number = Number(config.port.text);
			
			stage.removeChild(config);
			map = new Map();
			stage.addChild(map);
			player = new Player(keyHandler);
			allPlayers = new Object();
			allPlayers[0]=player;
			
			//client = new Client(adress, 5500, this.allPlayers, map);
			
			addEvents();
			map.addChild(player);
			//car = new Vehicule(keyHandler);
			
			//map.addChild(car);
			//car.addEvents();
			
			for (var i in Player)
				trace (i+"   "+player[i]);
			
			player.showGUI();
			player.addEvents();
			
			player.x = 400;
			player.y = 300;
			}
		
		public function addEvents()
			{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUp);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, getMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, getMouseUp);
			}
		
		public function setKeyDown(e:KeyboardEvent)
			{
			if (e.charCode == 0)
				this.keyHandler[e.keyCode] = true;
			else
				{
				this.keyHandler[String.fromCharCode(e.charCode)] = true;
				}
			
			if (e.keyCode==77)
				{
				map.width *= 0.9;
				map.height *= 0.9;
				
				map.x += map.width * 0.05;
				map.y += map.height * 0.05;
				}
			if (e.keyCode==80)
				{
				map.width *= 1.1;
				map.height *=1.1;
				map.x -= map.width * 0.05;
				map.y -= map.height * 0.05;
				}
			}
		
		public function setKeyUp(e:KeyboardEvent)
			{
			if (e.charCode == 0)
				this.keyHandler[e.keyCode] = false;
			else
				this.keyHandler[String.fromCharCode(e.charCode)] = false;
			}
		
		public function getMouseDown(e:MouseEvent)
			{
			if (e.target is Stage || e.target.parent is Stage)
				this.keyHandler[16] = true;
			}
		public function getMouseUp(e:MouseEvent)
			{
			this.keyHandler[16] = false;
			}
		}
		
	}